package ru.magicreader.client.view;

import ru.magicreader.client.I18nConstants;

import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.widgetideas.client.SpinnerListener;
import com.google.gwt.widgetideas.client.ValueSpinner;

public class SettingsPanel extends Composite {
	private final I18nConstants i18nc;
	private SettingsPanelListener listener;
	private ValueSpinner nEntriesSpinner;
	private FlexTable layout;

	public SettingsPanel(I18nConstants i18nc, SettingsPanelListener listener) {
		this.i18nc = i18nc;
		this.listener = listener;
		init();
	}
	
	private void init() {
		DecoratorPanel panel = new DecoratorPanel();
		layout = new FlexTable();
		layout.setCellSpacing(6);
		
		nEntriesSpinner = new ValueSpinner(5, 1, 10);
		nEntriesSpinner.getTextBox().setWidth("20px");
		nEntriesSpinner.getSpinner().addSpinnerListener(new SpinnerListener() {
			@Override
			public void onSpinning(long value) {
				listener.onNEntriesValueChanged(value);
			}
		});
		
		layout.setHTML(0, 0, i18nc.yourSettings() + ":"); 
		layout.getCellFormatter().setAlignment(0, 0, HasHorizontalAlignment.ALIGN_LEFT, HasVerticalAlignment.ALIGN_TOP);
		Label entriesToDisplayLabel = new Label(i18nc.entriesToDisplay() + ":");
		entriesToDisplayLabel.setTitle(i18nc.entriesToDisplayHint());
		layout.setWidget(1, 0, entriesToDisplayLabel);
		layout.setWidget(1, 1, nEntriesSpinner);
		
		panel.add(layout);
		
		initWidget(panel);
	}
	
	public void setNEntries(int n) {
		nEntriesSpinner.getTextBox().setValue(new Integer(n).toString());
	}
	
	public void setHeight(String height) {
		layout.setHeight(height);
	}
}
